package com.example.easyutils.joke;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.print.PrinterId;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.example.easyutils.BaseActivity;
import com.example.easyutils.R;
import com.example.easyutils.httputil.HttpGetRequest;
import com.example.easyutils.util.MyTypeAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

//
//https://way.jd.com/showapi/dtgxt?appkey=d841f29c61d4b1178380910774222055
public class JokeActivity extends BaseActivity {

    private RecyclerView mRecycleView;
    private LinearLayoutManager mLinearLayoutManager;
    private List handlerList;
    private static final int JOKE_SUCCESS_CODE=1,JOKE_ERROR_CODE=2,JOKE_WRAM_CODE=3;
    Handler handler = new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case JOKE_SUCCESS_CODE:
                    Log.d("success",handlerList+"");
                    JokeRecycleView mJokeRecycleView = new JokeRecycleView(handlerList,JokeActivity.this);
                    mLinearLayoutManager = new LinearLayoutManager(JokeActivity.this,RecyclerView.VERTICAL,false);
                    mRecycleView.setLayoutManager(mLinearLayoutManager);
                    mRecycleView.setAdapter(mJokeRecycleView);
                    break;
                case JOKE_ERROR_CODE:
                    break;
                case JOKE_WRAM_CODE:
                    break;
            }
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.joke_layout);
        mRecycleView = (RecyclerView)findViewById(R.id.jokeRecycleView);
//        添加Android自带的分割线
        mRecycleView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

        initJoke();

    }


    public void initJoke(){
//queryUIs[0].querySample: 2015-07-10
//queryUIs[0].queryName: time
//queryUIs[0].queryCondition: Optional
//queryUIs[1].querySample: 1
//queryUIs[1].queryName: page
//queryUIs[1].queryCondition: Optional
//queryUIs[2].querySample: 20
//queryUIs[2].queryName: maxResult
//queryUIs[2].queryCondition: Optional
//queryUIs[3].querySample: bd0592992b4d4050bfc927fe7a4db9f3
//queryUIs[3].queryName: showapi_sign
//queryUIs[3].queryCondition: Required
        Map map = new HashMap();
        HttpGetRequest.sendOkHttpGetRequst("https://way.jd.com/showapi/dtgxt?appkey=d841f29c61d4b1178380910774222055",
                new okhttp3.Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d("ERROR_JOKE",e+"");
                        handler.sendEmptyMessage(JOKE_WRAM_CODE);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Log.d("SUCCESS_JOKE",result);
                        Gson gson = new GsonBuilder()
                                .registerTypeAdapter(new TypeToken<Map<String, Object>>(){

                                }.getType(),new MyTypeAdapter()).create();
                        Map<String,Object> dataDetail = gson.fromJson(result,new TypeToken<Map<String,Object>>(){

                        }.getType());

                        Log.d("SUCCESS_DATA",result);
                        Log.d("data",((Map)((Map)dataDetail.get("result")).get("showapi_res_body")).get("contentlist")+"");
                        Log.d("datacode",dataDetail.get("code")+"");
                        String code = (String) dataDetail.get("code");
                        handlerList = new ArrayList();

                        if(code.equals("10000")){
                            handlerList = (List)((Map)((Map)dataDetail.get("result")).get("showapi_res_body")).get("contentlist");
                            handler.sendEmptyMessage(JOKE_SUCCESS_CODE);
                        }else{
                            handler.sendEmptyMessage(JOKE_WRAM_CODE);
                        }
                    }
                });
    }
}
